home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODUtil / Include / FWRect.h < prev    next >
Encoding:
Text File  |  1995-11-08  |  12.3 KB  |  355 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRect.h
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWRECT_H
  11. #define FWRECT_H
  12.  
  13. #ifndef FWPOINT_H
  14. #include "FWPoint.h"
  15. #endif
  16.  
  17. #if FW_LIB_EXPORT_PRAGMAS
  18. #pragma lib_export on
  19. #endif
  20.     
  21. //========================================================================================
  22. //    Typedefs
  23. //========================================================================================
  24.  
  25. typedef unsigned short FW_PointSelector;
  26.  
  27. const FW_PointSelector FW_kTopLeft = 0;
  28. const FW_PointSelector FW_kBotRight = 1;
  29.  
  30. //========================================================================================
  31. //    Forward Class Declarations
  32. //========================================================================================
  33.  
  34. class FW_CLASS_ATTR FW_CRect;
  35.  
  36. //========================================================================================
  37. //    Global
  38. //========================================================================================
  39.  
  40. extern const FW_CRect        FW_kZeroRect;
  41.  
  42. //========================================================================================
  43. //    Global operators << and >>
  44. //========================================================================================
  45.  
  46. FW_FUNC_ATTR const FW_CWritableStream& operator<<(const FW_CWritableStream& stream, const FW_CRect& rect);
  47. FW_FUNC_ATTR const FW_CReadableStream& operator>>(const FW_CReadableStream& stream, FW_CRect& rect);
  48.  
  49. //========================================================================================
  50. //    struct FW_SPlatformRect
  51. //========================================================================================
  52.  
  53. struct FW_CLASS_ATTR FW_SPlatformRect : public FW_PlatformRect
  54. {
  55.     FW_SPlatformRect();
  56.     
  57.     FW_SPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  58.                     FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  59.                     
  60.     FW_SPlatformRect(const FW_SPlatformPoint& topLeft, const FW_SPlatformPoint& botRight);
  61.     
  62.     FW_SPlatformRect(const FW_PlatformRect& plfmRect);
  63.     FW_SPlatformRect(const FW_SPlatformRect& plfmRect);
  64.     FW_SPlatformRect(const FW_CRect& rect);
  65.         
  66.     FW_SPlatformRect& operator=(const FW_SPlatformRect& plfmRect);
  67.     FW_SPlatformRect& operator=(const FW_PlatformRect& plfmRect);
  68.     FW_SPlatformRect& operator=(const FW_CRect& rect);
  69.     
  70.     const FW_SPlatformPoint&     TopLeft() const;
  71.     FW_SPlatformPoint&            TopLeft();
  72.     const FW_SPlatformPoint&     BotRight() const;
  73.     FW_SPlatformPoint&            BotRight();
  74.     
  75.     FW_Boolean            operator==(const FW_SPlatformRect& rect) const;
  76.     FW_Boolean            operator!=(const FW_SPlatformRect& rect) const;
  77.  
  78.     void Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  79.             FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  80. };
  81.  
  82. inline FW_SPlatformRect::FW_SPlatformRect() {left = right = top = bottom = 0;}
  83.  
  84. //========================================================================================
  85. // class FW_CRect
  86. //    Has the same layout as ODRect, but uses FW_CFixed instead of ODFixed
  87. //========================================================================================
  88.  
  89. class FW_CLASS_ATTR FW_CRect
  90. {
  91. //----------------------------------------------------------------------------------------
  92. //    Data members
  93. //
  94. public:
  95.     FW_CFixed left;
  96.     FW_CFixed top;
  97.     FW_CFixed right;
  98.     FW_CFixed bottom;
  99.  
  100. //----------------------------------------------------------------------------------------
  101. //    Constructors/Destructors
  102. //
  103. public:
  104.     FW_CRect();
  105.     FW_CRect(FW_CFixed l, FW_CFixed t, FW_CFixed r, FW_CFixed b);
  106.  
  107.     FW_CRect(const FW_PlatformRect& plfmRect);
  108.     FW_CRect(const FW_CRect& rect);
  109.     FW_CRect(const ODRect& rect);
  110.     
  111.     FW_CRect(const FW_CReadableStream& stream);
  112.     
  113.     FW_CRect(const FW_CPoint& topLeft, const FW_CPoint& botRight);
  114.     FW_CRect(const FW_CPoint& startPoint, const FW_CPoint& endPoint, FW_CFixed penSize);
  115.     FW_CRect(const FW_CPoint& topLeft, FW_CFixed width, FW_CFixed height);
  116.  
  117. //----------------------------------------------------------------------------------------
  118. //    operators
  119. //
  120. public:
  121.     // ----- Assignement -----    
  122.     FW_CRect& operator=(const FW_CRect& rect);
  123.     FW_CRect& operator=(const ODRect& odRect);
  124.     FW_CRect& operator=(const FW_PlatformRect& plfmRect);
  125.  
  126.     // ----- Modification -----
  127.     void    operator&=(const FW_CRect& rect);                // Intersect with rectangle
  128.     void    operator|=(const FW_CRect& rect);                // Union with rectangle
  129.     void    operator|=(const FW_CPoint& point);                // Expand to fit point
  130.     
  131.     // ----- Conversion -----
  132. //     It is too dangerous to have automatic conversion. Use AsPlatformRect.
  133. //    operator FW_PlatformRect() const;
  134.     operator ODRect() const
  135.         { return * (ODRect*) this; }
  136.     
  137.     // ----- Array -----
  138.     FW_CPoint& operator[](FW_PointSelector sel);        
  139.     const FW_CPoint& operator[](FW_PointSelector sel) const;
  140.     
  141.     // ----- Testing -----
  142.     FW_Boolean    operator==( const FW_CRect& ) const;
  143.     FW_Boolean    operator!=( const FW_CRect &r ) const
  144.                             {return !(*this==r);}
  145.     
  146. //----------------------------------------------------------------------------------------
  147. //    New API
  148. //
  149. public:
  150.     // ----- Conversion -----
  151.     void    AsPlatformRect(FW_PlatformRect& plfmRect) const;
  152.  
  153.     // ----- Modification -----
  154.     void    Clear();
  155.     void    Set(FW_CFixed left, FW_CFixed top, FW_CFixed right, FW_CFixed bottom);
  156.     void    Set(const FW_CPoint& topLeft, FW_CFixed width, FW_CFixed height);
  157.     void    SetInt(short left, short top, short right, short bottom);
  158.     void    Offset(FW_CFixed x, FW_CFixed y);
  159.     void    Offset(const FW_CPoint& offset);
  160.     void    Place(FW_CFixed x, FW_CFixed y);
  161.     void    Place(const FW_CPoint& offset);
  162.     void    Inset(FW_CFixed x, FW_CFixed y);
  163.     void    Map(const FW_CRect& srcRect, const FW_CRect& dstRect);
  164.     void    PlaceInCenter(const FW_CRect& srcRect);
  165.     
  166.     void    Sort();
  167.     
  168.     FW_CRect&        operator+=(const FW_CPoint& pt);
  169.     FW_CRect        operator+(const FW_CPoint& pt) const;
  170.  
  171.     FW_CRect&        operator-=(const FW_CPoint& pt);
  172.     FW_CRect        operator-(const FW_CPoint& pt) const;
  173.  
  174.     // ----- OpenDoc Transform -----
  175.  
  176.     void        Transform(Environment* ev, ODTransform* transform);
  177.     void        InverseTransform(Environment* ev, ODTransform* transform);
  178.         // Modify the rectangle
  179.  
  180.     FW_CRect    TransformCopy(Environment* ev, ODTransform* transform) const;
  181.     FW_CRect    InverseTransformCopy(Environment* ev, ODTransform* transform) const;
  182.         // Return a modified copy
  183.  
  184.     // ----- Accessors -----
  185.     
  186.     const FW_CPoint&     TopLeft() const
  187.                                 {return *(FW_CPoint*)&left;}
  188.     FW_CPoint&            TopLeft()
  189.                                 {return *(FW_CPoint*)&left;}
  190.     const FW_CPoint&     BotRight() const
  191.                                 {return *(FW_CPoint*)&right;}
  192.     FW_CPoint&            BotRight()
  193.                                 {return *(FW_CPoint*)&right;}
  194.     FW_CFixed        Width() const
  195.                                 {return FW_CFixed(right-left);}
  196.     FW_CFixed        Height() const
  197.                                 {return FW_CFixed(bottom-top);}
  198.     FW_CPoint            Size() const;
  199.     
  200.     // ----- Testing -----
  201.     FW_Boolean     IsEmpty() const;
  202.     FW_Boolean     Contains(const FW_CPoint& point) const;
  203.     FW_Boolean     Contains(const FW_CRect& rect) const;
  204.     FW_Boolean     IsIntersecting(const FW_CRect& rect) const;
  205.     
  206.     // ----- Operation on Rectangle -----
  207.     void         Intersection(const FW_CRect& rect);
  208.     void         Intersection(const FW_CRect& rect1, const FW_CRect& rect2);
  209.  
  210.     void         Union(const FW_CRect& rect);
  211.     void         Union(const FW_CRect& rect1, const FW_CRect& rect2);
  212. };
  213.  
  214. //========================================================================================
  215. //    Inlines
  216. //========================================================================================
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_SPlatformRect::FW_SPlatformRect
  220. //----------------------------------------------------------------------------------------
  221. inline FW_SPlatformRect::FW_SPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  222.                                             FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  223. {
  224.     left = l; top = t; 
  225.     right = r; bottom = b;
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_SPlatformRect::FW_SPlatformRect
  229. //----------------------------------------------------------------------------------------
  230. inline FW_SPlatformRect::FW_SPlatformRect(const FW_SPlatformPoint& topLeft, 
  231.                                           const FW_SPlatformPoint& botRight)
  232. {
  233. #ifdef FW_BUILD_MAC
  234.     left = topLeft.h; top = topLeft.v; 
  235.     right = botRight.h; bottom = botRight.v;
  236. #endif
  237. #ifdef FW_BUILD_WIN
  238.     left = topLeft.x; top = topLeft.y; 
  239.     right = botRight.x; bottom = botRight.y;
  240. #endif
  241.  
  242. //----------------------------------------------------------------------------------------
  243. //    FW_SPlatformRect::FW_SPlatformRect
  244. //----------------------------------------------------------------------------------------
  245. inline FW_SPlatformRect::FW_SPlatformRect(const FW_SPlatformRect& plfmRect)
  246. {
  247.     left = plfmRect.left; top = plfmRect.top; 
  248.     right = plfmRect.right; bottom = plfmRect.bottom;
  249.  
  250. //----------------------------------------------------------------------------------------
  251. //    FW_SPlatformRect::FW_SPlatformRect
  252. //----------------------------------------------------------------------------------------
  253. inline FW_SPlatformRect::FW_SPlatformRect(const FW_PlatformRect& plfmRect)
  254. {
  255.     left = plfmRect.left; top = plfmRect.top; 
  256.     right = plfmRect.right; bottom = plfmRect.bottom;
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    FW_SPlatformRect::operator=
  260. //----------------------------------------------------------------------------------------
  261. inline FW_SPlatformRect& FW_SPlatformRect::operator=(const FW_SPlatformRect& plfmRect)
  262. {
  263.     left = plfmRect.left; top = plfmRect.top; 
  264.     right = plfmRect.right; bottom = plfmRect.bottom;
  265.      return *this;
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    FW_SPlatformRect::operator=
  269. //----------------------------------------------------------------------------------------
  270. inline FW_SPlatformRect& FW_SPlatformRect::operator=(const FW_PlatformRect& plfmRect)
  271. {
  272.     left = plfmRect.left; top = plfmRect.top; 
  273.     right = plfmRect.right; bottom = plfmRect.bottom;
  274.      return *this;
  275.  
  276. //----------------------------------------------------------------------------------------
  277. //    FW_SPlatformRect::Set
  278. //----------------------------------------------------------------------------------------
  279. inline void FW_SPlatformRect::Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  280.                                     FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  281. {
  282.     left = l; top = t; 
  283.     right = r; bottom = b;
  284. }    
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    FW_SPlatformRect::TopLeft
  288. //----------------------------------------------------------------------------------------
  289. inline const FW_SPlatformPoint& FW_SPlatformRect::TopLeft() const
  290. {
  291.     return *(FW_SPlatformPoint*)this;
  292. }
  293.  
  294. //----------------------------------------------------------------------------------------
  295. //    FW_SPlatformRect::TopLeft
  296. //----------------------------------------------------------------------------------------
  297. inline FW_SPlatformPoint& FW_SPlatformRect::TopLeft()
  298. {
  299.     return *(FW_SPlatformPoint*)this;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. //    FW_SPlatformRect::BotRight
  304. //----------------------------------------------------------------------------------------
  305. inline const FW_SPlatformPoint& FW_SPlatformRect::BotRight() const
  306. {
  307. #ifdef FW_BUILD_MAC
  308.     return *(FW_SPlatformPoint*)⊥
  309. #endif
  310. #ifdef FW_BUILD_WIN
  311.     return *(FW_SPlatformPoint*)&right;
  312. #endif
  313. }
  314.  
  315. //----------------------------------------------------------------------------------------
  316. //    FW_SPlatformRect::BotRight
  317. //----------------------------------------------------------------------------------------
  318. inline FW_SPlatformPoint& FW_SPlatformRect::BotRight()
  319. {
  320. #ifdef FW_BUILD_MAC
  321.     return *(FW_SPlatformPoint*)⊥
  322. #endif
  323. #ifdef FW_BUILD_WIN
  324.     return *(FW_SPlatformPoint*)&right;
  325. #endif
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. //    FW_CRect:: operator&=
  330. //----------------------------------------------------------------------------------------
  331. inline void FW_CRect:: operator&=(const FW_CRect &r)
  332. {
  333.     Intersection(r);
  334. }
  335.  
  336. //----------------------------------------------------------------------------------------
  337. //    FW_CRect:: operator|=
  338. //----------------------------------------------------------------------------------------
  339. inline void FW_CRect:: operator|= (const FW_CRect &r)
  340. {
  341.     Union(r);
  342. }
  343.  
  344. #if FW_LIB_EXPORT_PRAGMAS
  345. #pragma lib_export off
  346. #endif
  347.  
  348. #endif
  349.